﻿
using System;

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;

public partial class sysadmin_DL_Project_AddSchool : PowerCreator.LMS.Web.Base.WebBasePage
{
    TOSchoolGroup obj = new TOSchoolGroup();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //{
        //    Script.RedirectURL(GetRefUrl());
        //}
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();
            BinderProvince();
            //BinderCity(ddl_Province.SelectedValue);
            //BinderRegion(ddl_City.SelectedValue);
            BinderStage();
            //BinderRule();
            BinderLevel();
            BinderSchoool();
            int SID = 0; 
            if (Request.QueryString["SID"] != null&&Request.QueryString["Action"] != null)
            {
                SID = int.Parse(Request.QueryString["SID"].ToString());
                ViewState["SID"] = SID.ToString();
                ViewState["Action"] = Request.QueryString["Action"].ToString();
                if (ViewState["Action"].ToString() == "edit")
                    BinderForm(SID);
                else
                    ddl_SchoolGroup.SelectedValue = SID.ToString();
            }

        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 2)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }

        }

    }
    private bool CheckPopedom()
    {
        bool rt = false;

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login != null)
        {
            if (login.VOAdvancedUser.AUserGroup == 1)
            {
                rt = true;
            }
        }
        return rt;

    } 
    protected override void InitPage()
    {
        //lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddJob_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobName.Text = GetLanageValueByKey("lbl_AddJob_JobName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobDescription.Text = GetLanageValueByKey("lbl_AddJob_JobDescription", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        //base.InitPage();

        //lbl_InfoTitle_AddNewType.Text = GetLanageValueByKey("lab_AddNewType_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_NewTypeName.Text = GetLanageValueByKey("lab_AddNewType_TypeName", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }


    private void BinderProvince()
    {
        ddl_Province.Items.Clear();
        TOProvince objPro = new TOProvince();
        DataTable dt = new DataTable();

        dt = objPro.Query("");
        ddl_Province.DataSource = dt;
        ddl_Province.DataTextField = "Province";
        ddl_Province.DataValueField = "Province";
        ddl_Province.DataBind();

        BinderCity(ddl_Province.SelectedValue);
        //if (voCity != null)
        //    ddl_Province.SelectedValue = voCity.ProID.ToString();
    }
    private void BinderCity(string province)
    {
        ddl_City.Items.Clear();
        TOCity objCity = new TOCity();
        DataTable dt = new DataTable();
        if (province != "0")
            dt = objCity.Query("ProID=(select top 1 ID from TO_Province where Province='" + province + "')");
        else
            dt = objCity.Query("");


        ddl_City.DataSource = dt;
        ddl_City.DataTextField = "City";
        ddl_City.DataValueField = "City";
        ddl_City.DataBind();
        BinderRegion(ddl_City.SelectedValue);
        //if (cid != 0)
        //    ddl_City.SelectedValue = cid.ToString();
    }


    private void BinderRegion(string city)
    {
        ddl_Region.Items.Clear();
        TORegion objRegion = new TORegion();
        DataTable dt = new DataTable();
        string search = " 1=1 ";
        if (city !="0")
            search = search + " and CityID=(select top 1 ID from TO_City where City='" + city + "')";

        if (!CheckPopedom())
            search = search + " and ManagerID=" + login.UserID;

        dt = objRegion.Query(search);
        ddl_Region.DataSource = dt;
        ddl_Region.DataTextField = "Region";
        ddl_Region.DataValueField = "Region";
        ddl_Region.DataBind();
    }


    private void BinderStage()
    {
        TOSchoolStage objStage = new TOSchoolStage();
        DataTable dt = new DataTable();
        dt = objStage.Query("");


        ddl_Stage.DataSource = dt;
        ddl_Stage.DataTextField = "SchoolStage";
        ddl_Stage.DataValueField = "ID";
        ddl_Stage.DataBind();
        //if (cid != 0)
        //    ddl_City.SelectedValue = cid.ToString();
    }

    private void BinderLevel()
    {
        ddl_Level.Items.Clear();
        ListItem li1 = new ListItem();
        li1.Text = "普通成员校";
        li1.Value = "01";
        ddl_Level.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = "各区分校/进修学校";
        li2.Value = "02";
        ddl_Level.Items.Add(li2);
        if (CheckPopedom())
        {
            ListItem li3 = new ListItem();
            li3.Text = "培训校";
            li3.Value = "03";
            ddl_Level.Items.Add(li3);
        }
        
    }
    //private void BinderRule()
    //{
    //    TORule objRule = new TORule();
    //    DataTable dt = new DataTable();
    //    dt = objRule.Query("");


    //    ddl_Rule.DataSource = dt;
    //    ddl_Rule.DataTextField = "RuleName";
    //    ddl_Rule.DataValueField = "Rule_Id";
    //    ddl_Rule.DataBind();
    //    //if (cid != 0)
    //    //    ddl_City.SelectedValue = cid.ToString();
    //}
    private void BinderSchoool()
    {
        TOSchoolGroup objSchool = new TOSchoolGroup();
        //DataTable dt = new DataTable();
        //dt = objSchool.Query("");
        //ddl_SchoolGroup.DataSource = dt;
        //ddl_SchoolGroup.DataTextField = "SchoolGroupName";
        //ddl_SchoolGroup.DataValueField = "SchoolGroup_Id";
        //ddl_SchoolGroup.DataBind();
        //ListItem li = new ListItem();
        //li.Text = "顶级节点";
        //li.Value = "0";
        //ddl_SchoolGroup.Items.Insert(0, li);

        string search = " ";
        DataTable dt = objSchool.Query(search);
        //ArrayList list = new ArrayList();
        //list = objSchool.GetSchoolGroupTree(dt);
        DataTable dt2 = new DataTable();
        dt2 = objSchool.GetSchoolGroupTree(dt);
        if (CheckPopedom())
        {
            DataRow dr = dt2.NewRow();
            dr["Text"] = "默认学校";
            dr["Value"] = "0";
            dr["SchoolGroupRegion"] = "0";
            dr["SchoolGroupLevel"] = "03";
            dt2.Rows.Add(dr);
        }


        ddl_SchoolGroup.DataSource = dt2;
        ddl_SchoolGroup.DataTextField = "Text";
        ddl_SchoolGroup.DataValueField = "Value";
        ddl_SchoolGroup.DataBind();

    }



    private void BinderForm(int id)
    {
        VOTOSchoolGroup vo = new VOTOSchoolGroup();
        vo = obj.Load(id);

        txt_School.Text = vo.SchoolGroupName;
        ddl_Province.SelectedValue = vo.SchoolGroupProvince;
        BinderCity(vo.SchoolGroupProvince);
        ddl_City.SelectedValue = vo.SchoolGroupCity;
        BinderRegion(vo.SchoolGroupCity);
        ddl_Region.SelectedValue = vo.SchoolGroupRegion;
        ddl_Stage.SelectedValue = vo.SchoolGroupStageID.ToString();
        ddl_Level.SelectedValue = vo.SchoolGroupLevel;
        //ddl_Rule.SelectedValue = vo.SchoolGroupRule;
        ddl_SchoolGroup.SelectedValue = vo.SchoolGroupParentId.ToString();
        //txt_SchoolGroupNum.Text = vo.SchoolGroupNum;
        txt_LinkMan.Text = vo.SchoolGroupLinkMan;
        txt_LinkTel.Text = vo.SchoolGroupLinkTel;
        txt_LinkEmail.Text = vo.SchoolGroupLinkEmail;
    }
    private bool CheckPageData()
    {
        string search = string.Format("SchoolGroupName = '{0}'", txt_School.Text.Trim());
        if (ViewState["SID"] != null)
        {
            search += "and SchoolGroup_Id<>" + ViewState["SID"].ToString();
        }
        if (obj.Count(search) != 0)
        {

            //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);

            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddNewType_checkMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTOSchoolGroup vo = new VOTOSchoolGroup();
        if (ViewState["Action"].ToString()=="add")
        {
            vo.SchoolGroupName= txt_School.Text.Trim();
            vo.SchoolGroupProvince = ddl_Province.SelectedValue;
            vo.SchoolGroupCity = ddl_City.SelectedValue;
            vo.SchoolGroupRegion = ddl_Region.SelectedValue;
            vo.SchoolGroupStageID= int.Parse(ddl_Stage.SelectedValue);
            vo.SchoolGroupLevel=ddl_Level.SelectedValue;
            //vo.SchoolGroupRule = 0;
            vo.SchoolGroupParentId = int.Parse(ddl_SchoolGroup.SelectedValue);
            //vo.SchoolGroupNum = txt_SchoolGroupNum.Text.Trim();
            vo.SchoolGroupLayer = obj.QuerySchoolGroupLayer(int.Parse(ddl_SchoolGroup.SelectedValue.ToString()));
            vo.SchoolGroupLinkMan = txt_LinkMan.Text.Trim();
            vo.SchoolGroupLinkTel = txt_LinkTel.Text.Trim();
            vo.SchoolGroupLinkEmail = txt_LinkEmail.Text.Trim();
            vo.CreatorId = GetLoginEntity.UserID;
            vo.CreatorName = GetLoginEntity.UserName;
            if (obj.Add(vo))
            {
                string url = "ManageSchool.aspx";
                Script.RedirectURL(url);
            }
        }
        else if(ViewState["Action"].ToString()=="edit")
        {
            vo = obj.Load(int.Parse(ViewState["SID"].ToString()));
            vo.SchoolGroupName = txt_School.Text.Trim();
            vo.SchoolGroupProvince = ddl_Province.SelectedValue;
            vo.SchoolGroupCity = ddl_City.SelectedValue;
            vo.SchoolGroupRegion = ddl_Region.SelectedValue;
            vo.SchoolGroupStageID = int.Parse(ddl_Stage.SelectedValue);
            vo.SchoolGroupLevel = ddl_Level.SelectedValue;
            //vo.SchoolGroupRule = 0;
            vo.SchoolGroupParentId = int.Parse(ddl_SchoolGroup.SelectedValue);
            //vo.SchoolGroupNum = txt_SchoolGroupNum.Text.Trim();
            vo.SchoolGroupLayer = obj.QuerySchoolGroupLayer(int.Parse(ddl_SchoolGroup.SelectedValue.ToString()));
            vo.SchoolGroupLinkMan = txt_LinkMan.Text.Trim();
            vo.SchoolGroupLinkTel = txt_LinkTel.Text.Trim();
            vo.SchoolGroupLinkEmail = txt_LinkEmail.Text.Trim();
            if (obj.Update(vo))
            {
                string url = "ManageSchool.aspx";
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManageSchool.aspx";
        Script.RedirectURL(url);
    }



    protected void ddl_Province_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinderCity(ddl_Province.SelectedValue);
    }


    protected void ddl_City_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinderRegion(ddl_City.SelectedValue);
    }

}
